const express = require('express')
const router = express.Router()
const superagent = require('superagent');
const cheerio = require('cheerio');

let findImg = RegExp(/data-original="(.*?)"/)
let findHelf = RegExp(/href="(.*?)" title/)
let findTitle= RegExp(/title="(.*?)"/)
let findActor= RegExp(/hidden-xs">(.*?)<\/div>/)
let findScore= RegExp(/class="score">(.*?)<\/span>/)

function unChange(val) {
  return unescape(val.replace(/&#x/g,'%u').replace(/;/g,''))
};
router.get('/',(req,res)=>{
  console.log(req.query.page);
  let page = req.query.page
  const baseUrl = 'https://www.jisuysw.com/index.php?s=home-vod-type-id-2-area--year--letter--order-hits-picm-1-p-'
  let gourl = baseUrl + page
  let arr = []
  new Promise((resolve,reject) =>{
    superagent.get(gourl, function(err, resulst) {
        if (!err) {
            console.log('抓取成功');
            resolve(resulst.text)
        }else{
          reject(err)
        }
      }).set({'cookie':'UM_distinctid=1720cba66a045a-08b0bb4097cfc2-f313f6d-1fa400-1720cba66a1793; zanpian_playlog=think%3A%7B%22id_59169%22%3A%7B%22log_vid%22%3A%2259169%22%2C%22log_sid%22%3A%221%22%2C%22log_pid%22%3A%221%22%2C%22log_urlname%22%3A%22%25E7%25AC%25AC01%25E9%259B%2586%22%2C%22log_maxnum%22%3A%221%22%2C%22log_addtime%22%3A%221589351462%22%7D%2C%22id_792%22%3A%7B%22log_vid%22%3A%22792%22%2C%22log_sid%22%3A%222%22%2C%22log_pid%22%3A%221%22%2C%22log_urlname%22%3A%22%25E7%25AC%25AC01%25E9%259B%2586%22%2C%22log_maxnum%22%3A%2210%22%2C%22log_addtime%22%3A%221589351534%22%7D%2C%22id_57968%22%3A%7B%22log_vid%22%3A%2257968%22%2C%22log_sid%22%3A%221%22%2C%22log_pid%22%3A%221%22%2C%22log_urlname%22%3A%22%25E7%25AC%25AC01%25E9%259B%2586%22%2C%22log_maxnum%22%3A%221%22%2C%22log_addtime%22%3A%221589351624%22%7D%2C%22id_16959%22%3A%7B%22log_vid%22%3A%2216959%22%2C%22log_sid%22%3A%224%22%2C%22log_pid%22%3A%221%22%2C%22log_urlname%22%3A%22%25E7%25AC%25AC01%25E9%259B%2586%22%2C%22log_maxnum%22%3A%2225%22%2C%22log_addtime%22%3A%221589356735%22%7D%2C%22id_31115%22%3A%7B%22log_vid%22%3A%2231115%22%2C%22log_sid%22%3A%221%22%2C%22log_pid%22%3A%221%22%2C%22log_urlname%22%3A%22%25E7%25AC%25AC01%25E9%259B%2586%22%2C%22log_maxnum%22%3A%2210%22%2C%22log_addtime%22%3A%221589354891%22%7D%2C%22id_16968%22%3A%7B%22log_vid%22%3A%2216968%22%2C%22log_sid%22%3A%224%22%2C%22log_pid%22%3A%221%22%2C%22log_urlname%22%3A%22%25E7%25AC%25AC01%25E9%259B%2586%22%2C%22log_maxnum%22%3A%2210%22%2C%22log_addtime%22%3A%221589354962%22%7D%2C%22id_59152%22%3A%7B%22log_vid%22%3A%2259152%22%2C%22log_sid%22%3A%221%22%2C%22log_pid%22%3A%221%22%2C%22log_urlname%22%3A%22%25E7%25AC%25AC01%25E9%259B%2586%22%2C%22log_maxnum%22%3A%2213%22%2C%22log_addtime%22%3A%221589357556%22%7D%2C%22id_50270%22%3A%7B%22log_vid%22%3A%2250270%22%2C%22log_sid%22%3A%222%22%2C%22log_pid%22%3A%221%22%2C%22log_urlname%22%3A%22HD1280%25E9%25AB%2598%25E6%25B8%2585%25E5%259B%25BD%25E8%25AF%25AD%25E4%25B8%25AD%25E5%25AD%2597%25E7%2589%2588%22%2C%22log_maxnum%22%3A%221%22%2C%22log_addtime%22%3A%221589357676%22%7D%2C%22id_41766%22%3A%7B%22log_vid%22%3A%2241766%22%2C%22log_sid%22%3A%224%22%2C%22log_pid%22%3A%223%22%2C%22log_urlname%22%3A%22%25E7%25AC%25AC03%25E9%259B%2586%22%2C%22log_maxnum%22%3A%2224%22%2C%22log_addtime%22%3A%221589424592%22%7D%2C%22id_576%22%3A%7B%22log_vid%22%3A%22576%22%2C%22log_sid%22%3A%222%22%2C%22log_pid%22%3A%221%22%2C%22log_urlname%22%3A%22%25E7%25AC%25AC01%25E9%259B%2586%22%2C%22log_maxnum%22%3A%2215%22%2C%22log_addtime%22%3A%221589448356%22%7D%7D; PHPSESSID=ldbmg19n1bi3qn4mttn0et3bs5; CNZZDATA1256703521=2034412301-1589348965-https%253A%252F%252Fwww.baidu.com%252F%7C1589700179; close=1; channel=qq; axcaccess=d24f6ff46806ce2b1ca562f4813a26fe; user_auth=think%3A%7B%22user_id%22%3A%2213527%22%2C%22user_name%22%3A%22GNzoUp%22%2C%22user_nickname%22%3A%22GNzoUp%22%2C%22user_email%22%3A%22%22%2C%22user_isemail%22%3A0%2C%22user_last_login_time%22%3A%221589702816%22%7D; user_auth_sign=0bf38cb7e2f39c5d46c704ec590e98338433c672'})
  })
  .then(val=>{
    let $ = cheerio.load(val)
    let arr = []
    $('#content li').each((i,ele)=>{
      let obj = {}
      let html = unChange($(ele).html())
      obj.href = html.match(findHelf)[1]
      obj.img = html.match(findImg)[1]
      obj.score = html.match(findScore)[1]
      obj.name = html.match(findTitle)[1]
      obj.actor = html.match(findActor)[1].replace(/%uB7/g,'')
      arr.push(obj)
      
    })
    res.send(JSON.stringify(arr))

  })
  .catch(err=>{
    res.send(err.message)
  })
  
})




module.exports = router